package com.syqc.monitor.ui.cost

import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import autodispose2.autoDispose
import com.syqc.adapter.ChoosePhotoAdapter
import com.syqc.comlib.rx.doAfterChanged
import com.syqc.comlib.rx.filterFast
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentTrafficBinding
import com.syqc.monitor.databinding.IncludeDescPhotosBinding
import com.syqc.monitor.databinding.IncludeRecyclerWrapBinding
import com.syqc.monitor.databinding.IncludeSelectSureBinding
import com.syqc.utils.InputUtil

class TrafficFragment : BaseFeeFragment<FragmentTrafficBinding>(FragmentTrafficBinding::inflate) {
    private lateinit var descBinding: IncludeDescPhotosBinding
    private lateinit var mRecyclerVB: IncludeRecyclerWrapBinding
    private lateinit var sureBinding: IncludeSelectSureBinding
    private val mAdapter by lazy { ChoosePhotoAdapter() }
    private val mVM by viewModels<TrafficViewModel>()
    override val mViewModel: BaseFeeViewModel
        get() = mVM
    override val isOil: Boolean
        get() = false

    override fun setDate(type: Int, time: String) {
        binding.lrvDate.setRightText(time)
        mVM.update = time
    }

    override fun uploadData() {
        mVM.uploadTraffic()
    }

    override fun clearData() {
        binding.uetSpeed.setText("")
        binding.uetBridge.setText("")
        binding.uetStop.setText("")
        binding.uetWash.setText("")
        binding.uetTotal.setText("0.00")
        binding.uetOther.setText("")
        descBinding.editFeeRemark.setText("")
        mVM.clearData()
        binding.lrvDate.setRightText(mVM.update)
        binding.nsView.fullScroll(View.FOCUS_UP)

    }

    override fun initView(view: View) {
        descBinding = IncludeDescPhotosBinding.bind(binding.root)
        sureBinding = IncludeSelectSureBinding.bind(binding.root)
        sureBinding.tvSure.setText(R.string.desc_com_sure)
        focusChange(binding.uetSpeed.getEditText())
        focusChange(binding.uetBridge.getEditText())
        focusChange(binding.uetStop.getEditText())
        focusChange(binding.uetWash.getEditText())
        focusChange(binding.uetOther.getEditText())
        binding.uetTotal.getEditText().isEnabled = false

        InputUtil.editTextNested(descBinding.editFeeRemark)

        binding.lrvDate.setRightText(mVM.update)

        mRecyclerVB = IncludeRecyclerWrapBinding.bind(binding.root)
        mRecyclerVB.recyclerWrap.layoutManager = GridLayoutManager(requireContext(), 3)
        mRecyclerVB.recyclerWrap.adapter = mAdapter

    }

    override fun initData() {
        super.initData()
        mVM.handlePhotos(null)

        mVM.feeTotalLive.observe(this) {
            binding.uetTotal.setText(it)
        }

        mVM.photoLive.observe(this) {
            mAdapter.setList(it)
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun initEvent() {
        binding.lrvDate.setOnClickListener {
            mDatePicker.show(mVM.update)
        }

        binding.uetSpeed.setOnTextChange {
            val str = isNumOk(binding.uetSpeed, it)
            mVM.feeSpeed = str
        }

        binding.uetBridge.setOnTextChange {
            val str = isNumOk(binding.uetBridge, it)
            mVM.feeBridge = str
        }

        binding.uetStop.setOnTextChange {
            val str = isNumOk(binding.uetStop, it)
            mVM.feeStop = str
        }

        binding.uetWash.setOnTextChange {
            val str = isNumOk(binding.uetWash, it)
            mVM.feeWash = str
        }

        binding.uetOther.setOnTextChange {
            val str = isNumOk(binding.uetOther, it)
            mVM.feeOther = str
        }

        sureBinding.tvSure.filterFast {
            mVM.uploadTraffic()
        }
        descBinding.editFeeRemark.doAfterChanged {
            mVM.remark = it
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val path = mAdapter.data[position]
            if (path.isEmpty()) {
                takePhoto()
            }
        }

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            mViewModel.removePhoto(position)
        }
    }
}